package com.user.query.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.user.query.model.Menu;
import com.user.query.service.MenuService;
import com.user.query.utils.ConstantUtils;
import com.user.query.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

/**
 * 菜单控制层MenuController
 * @author lijk
 * @date 2015-10-27 下午5:27:39
 */
@Controller
@RequestMapping("/menu/")
public class MenuController extends BaseController {

	@Resource
	private MenuService menuService;
	
	
	/**
	 * 获取所有菜单信息（json格式）
	 * @return
	 */
	@RequestMapping("listJson")
	@ResponseBody
	public Map<String, Object> listJson() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Menu menu = new Menu();
			menu.setStatus(ConstantUtils.Status.OK);
			menu.setOrderByClause("sort");
			
			resultMap.put("list", JSON.toJSONString(menuService.grep(menu)));
		} catch (Exception e) {
			log.info("获取所有菜单异常：" + e);
		}
		return resultMap;
	}

	/**
	 * 更新菜单信息 
	 * @param menu
	 * @return
	 */
	@RequestMapping("modifyOrAdd")
	@ResponseBody
	public ModelMap modifyOrAdd(Menu menu) {
		ModelMap resultMap = new ModelMap();
		boolean code = false; 
		String msg = "";
		try {
			menuService.modifyOrAdd(menu);
			code = true;
			msg = "菜单更新成功";
		} catch (Exception e) {
			log.info("菜单更新异常：" + e);
			msg = "菜单操作异常";
		}
		resultMap.put("code", code);
		resultMap.put("msg", msg);
		return resultMap;
	}
	
	/**
	 * 获取菜单记录
	 * @param id
	 * @return
	 */
	@RequestMapping("getJson")
	@ResponseBody
	public Map<String, Object> getJson(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap.put("record", JSON.toJSONString(menuService.get(id)));
		} catch (Exception e) {
			log.info("获取菜单信息异常：" + e);
		}
		return resultMap;
	}
	
	/**
	 * 菜单分页
	 * @param request
	 * @param menu
	 * @param pageNo
	 * @return
	 */
	@RequestMapping("queryPage")
	public ModelAndView queryPage(HttpServletRequest request, Menu menu, 
			@RequestParam(required = false, defaultValue = "1")Integer pageNo) {
		ModelAndView view = new ModelAndView("/system/menu");
		try {
			menu.setStatus(ConstantUtils.Status.OK);
			Page<Menu> page = new Page<Menu>();
			page.setPageNo(pageNo);
			page.setQueryParam(menu);
			page.setOrderByClause("sort");
			
			view.addObject("page", menuService.queryPage(page));
			
			List<Menu>  menus = menuService.queryAllByParentId(null);
			view.addObject("menus", menus);
		} catch (Exception e) {
			log.info("菜单分页异常：" + e);
		}
		return view;
	}
	
	@RequestMapping("initContentNav")
	public ModelAndView initContentNav(String menuId) {
		ModelAndView view = new ModelAndView("/comm/content");
		try {
			view.addObject("record", menuService.get(menuId));
		} catch (Exception e) {
			log.info("主页面初始异常：" + e);
		}
		return view;
	}
}
